<template>
  <div class="createPost-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      class="form-container"
    >
      <sticky :z-index="10" :class-name="'sub-navbar published'">
        <el-button
          v-loading="loading"
          style="margin-left: 80%"
          type="success"
          @click="submitForm"
        >
          发布
        </el-button>
        <el-button
          v-loading="loading"
          style="margin-left: 5px"
          type="success"
          @click="saveForm"
        >
          保存
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">
            <el-form-item
              style="margin-bottom: 40px"
              label-width="70px"
              label="标题:"
            >
              <el-input
                v-model="postForm.title"
                :rows="1"
                type="textarea"
                class="article-textarea"
                autosize
                placeholder="Please enter the title"
              />
              <span v-show="contentShortLength" class="word-counter"
                >{{ contentShortLength }}words</span
              >
            </el-form-item>
            <div class="postInfo-container">
              <el-row>
                <el-col :span="6">
                  <el-form-item
                    label-width="60px"
                    label=""
                    class="postInfo-container-item"
                  >
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label-width="60px"
                    label=""
                    class="postInfo-container-item"
                  >
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label-width="60px"
                    label=""
                    class="postInfo-container-item"
                  >
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label-width="60px"
                    label=""
                    class="postInfo-container-item"
                  >
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
            <div>
              <el-input
                type="textarea"
                :autosize="{ minRows: 15, maxRows: 50 }"
                placeholder="请输入内容"
                v-model="postForm.content"
                style=""
              >
              </el-input>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/SingleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validURL } from '@/utils/validate'
import { updateNotice, saveNotice, fetchDataDetail} from '@/api/notice'
import store from '@/store'

const defaultForm = {
  status: '',
  title: '', // 文章题目
  content: '', // 文章内容
  id: 0,
  
}

export default {
  name: 'ArticleDetail',
  components: {
    Tinymce,
    MDinput,
    Upload,
    Sticky
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
        if (validURL(value)) {
          callback()
        } else {
          this.$message({
            message: '外链url填写不正确',
            type: 'error'
          })
          callback(new Error('外链url填写不正确'))
        }
      } else {
        callback()
      }
    }
    return {
      textarea2: '',
      loading: false,
      postForm: Object.assign({}, defaultForm),
      rules: {
        image_uri: [{ validator: validateRequire }],
        documentName: [{ validator: validateRequire }],
        // content: [{ validator: validateRequire }],
        source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
      },
      tempRoute: {}
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.title.length
    },
    // lang() {
    //   return this.$store.getters.language
    // },
    displayTime: {
      // set and get is useful when the data
      // returned by the back end api is different from the front end
      // back end return => "2013-06-25 06:59:25"
      // front end need timestamp => 1372114765000
      get() {
        return +new Date(this.postForm.display_time)
      },
      set(val) {
        this.postForm.display_time = new Date(val)
      }
    }
  },
  created() {
    // this.tempRoute = Object.assign({}, this.$route)
    const id = this.$route.params && this.$route.params.id
    this.getData(id)
    console.log('pf:'+this.postForm)
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit()
    },
    getData(id) {
      this.listLoading = true
      fetchDataDetail(id).then(response => {
        this.postForm = response.data
        
        
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    submitForm() {
      console.log(store.getters.token)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          updateNotice(this.postForm, store.getters.token).then(() => {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    saveForm() {
      console.log(store.getters.token)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true

          saveNotice(this.postForm, store.getters.token).then(() => {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })

          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/mixin.scss';

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
